package org.example;

import java.util.Scanner;

public class Main {
    private static AdminService adminService = new AdminService();
    private static ManagerService managerService = new ManagerService(adminService);
    private static ReceptionService receptionService = new ReceptionService(managerService);
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("=== 欢迎使用影院管理系统 ===");

        try {
            mainMenu();
        } finally {
            // 程序退出时保存所有数据
            adminService.saveData();
            managerService.saveData();
            System.out.println("数据已保存，程序退出");
        }
    }

    private static void mainMenu() {
        while (true) {
            System.out.println("\n=== 主菜单 ===");
            System.out.println("1.管理员登录");
            System.out.println("2.经理登录");
            System.out.println("3.前台登录");
            System.out.println("4.退出系统");
            System.out.print("请选择：");

            try {
                int choice = Integer.parseInt(scanner.nextLine().trim());
                switch (choice) {
                    case 1:
                        adminLogin();
                        break;
                    case 2:
                        managerLogin();
                        break;
                    case 3:
                        receptionLogin();
                        break;
                    case 4:
                        return; // 退出程序
                    default:
                        System.out.println("无效选择（1-4）");
                }
            } catch (NumberFormatException e) {
                System.out.println("请输入数字（1-4）");
            }
        }
    }

    private static void adminLogin() {
        System.out.println("\n=== 管理员登录 ===");
        System.out.print("用户名：");
        String user = scanner.nextLine().trim();
        System.out.print("密码：");
        String pwd = scanner.nextLine().trim();

        adminService.login(user, pwd);
        if (adminService.isLoggedIn()) {
            adminService.showAdminMenu();
        }
    }

    private static void managerLogin() {
        System.out.println("\n=== 经理登录 ===");
        System.out.print("用户名：");
        String user = scanner.nextLine().trim();
        System.out.print("密码：");
        String pwd = scanner.nextLine().trim();

        User loginUser = adminService.verifyUser(user, pwd, "经理");
        if (loginUser != null) {
            System.out.println("经理登录成功！");
            managerService.setCurrentManager(loginUser);
            managerService.showManagerMenu();
        } else {
            System.out.println("登录失败：用户名或密码错误");
        }
    }

    private static void receptionLogin() {
        System.out.println("\n=== 前台登录 ===");
        System.out.print("用户名：");
        String user = scanner.nextLine().trim();
        System.out.print("密码：");
        String pwd = scanner.nextLine().trim();

        User loginUser = adminService.verifyUser(user, pwd, "前台");
        if (loginUser != null) {
            System.out.println("前台登录成功！");
            receptionService.setCurrentReception(loginUser);
            receptionService.showReceptionMenu();
        } else {
            System.out.println("登录失败：用户名或密码错误");
        }
    }
}